package com.tang.algorithm.swordFingerOffer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author tlm
 * @version 1.0
 * @date 2020/8/2 20:47
 */
public class sword32 {
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(3);
        TreeNode treeNode2 = new TreeNode(9);
        TreeNode treeNode3 = new TreeNode(20);
        TreeNode treeNode4 = new TreeNode(15);
        TreeNode treeNode5 = new TreeNode(7);
        treeNode1.left = null;
        treeNode1.right = treeNode2;
        treeNode2.right = null;
        treeNode2.left = treeNode3;
        treeNode3.left = treeNode4;
        treeNode3.right = treeNode5;
        levelOrder(treeNode1);

    }

    public static int[] levelOrder(TreeNode root) {
        //当树的根节点为空，则直接返回空列表 [] ；
        if (root == null) {
            return new int[0];
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        ArrayList<Integer> res = new ArrayList<>();
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            res.add(node.val);
            if (node.left != null) queue.add(node.left);
            if (node.right != null) queue.add(node.right);

        }
        int[] result = new int[res.size()];
        for (int i = 0; i < res.size(); i++) {
            result[i] = res.get(i);
        }
        return result;
    }
}
